//
//  CollectionSearchViewController.m
//  Delicious2
//
//  Created by FSD on 16/1/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CollectionSearchViewController.h"
#import "EatShowViewController.h"
@interface CollectionSearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, retain) DataBaseHandle *dataBaseHandle;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSMutableArray *resultDataSource;
@end

@implementation CollectionSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.resultDataSource = [NSMutableArray arrayWithCapacity:0];
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    self.dataSource = [self.dataBaseHandle selectDataBase:nil];
    for (NSDictionary *dic in self.dataSource) {
        MineModel *mineModel = [[MineModel alloc] initWithDictionary:dic];
        [self.resultDataSource addObject:mineModel];
    }
    [self createSearchBar];
    [self createTableView];
}

- (void)viewWillAppear:(BOOL)animated {

    [self.view nightMode];
    [self.tableView nightMode];
}

#pragma mark - 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[FSDCollectionTableViewCell class] forCellReuseIdentifier:@"FSDCollectionTableViewCellIdentifier"];
    self.tableView.tableHeaderView = self.searchBar;
    [self.view addSubview:self.tableView];
}

#pragma mark - 创建UISearchBar
- (void)createSearchBar {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    self.searchBar.delegate = self;
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.placeholder = @"请输入关键字";
    self.searchBar.keyboardType = UIKeyboardTypeNamePhonePad;
}

#pragma mark - TableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FSDCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FSDCollectionTableViewCellIdentifier"];
    [cell nightMode];
    if (indexPath.row < self.resultDataSource.count) {
        cell.mineModel = self.resultDataSource[indexPath.row];
    }
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.resultDataSource.count;
    //    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    EatShowViewController *show = [[EatShowViewController alloc] init];
    MineModel *mineModel = self.resultDataSource[indexPath.row];
    show.urlTitle = mineModel.title;
    show.urlImage = mineModel.image;
    show.urlString = mineModel.url;
    show.urlKind = mineModel.kind;
    [self.navigationController pushViewController:show animated:YES];
}

#pragma mark - UISearchDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    for (id cc in [searchBar subviews]) {
        if ([cc isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)cc;
            [button setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self.resultDataSource removeAllObjects];
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    self.dataSource = [self.dataBaseHandle selectSearchDataBase:searchText];
    for (NSDictionary *dic in self.dataSource) {
        MineModel *mineModel = [[MineModel alloc] initWithDictionary:dic];
        [self.resultDataSource addObject:mineModel];
    }
    [self.tableView reloadData];
    [self.dataBaseHandle closeDataBase];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
